unit uDMHelper;

interface

uses
  SysUtils, Classes, DB, ADODB;

type
  TdbHelper = class(TDataModule)
    conSql: TADOConnection;
    cmdSql: TADOCommand;
    adsTemp: TADODataSet;
    procedure DataModuleCreate(Sender: TObject);
  private


    { Private declarations }
  public
    { Public declarations }
    procedure ExecuteSql(SqlText: string);
    function OpenDataSet(aDataSet: TADODataSet; SqlText: string): Integer;
    function IsEmpty(SqlText: string): Boolean;
    function GetFirstFieldValue(SqlText: string): string;
  end;

var
  dbHelper: TdbHelper;

implementation

{$R *.dfm}

{ TdbHelper }

procedure TdbHelper.ExecuteSql(SqlText: string);
begin
  conSql.Execute(SqlText);
end;


function TdbHelper.OpenDataSet(aDataSet: TADODataSet; SqlText: string): Integer;
begin
  if aDataSet.Active then aDataSet.Close;
  aDataSet.Connection := conSql;
  aDataSet.CommandText := SqlText;
  aDataSet.Open;
  Result := aDataSet.RecordCount;
end;

function TdbHelper.GetFirstFieldValue(SqlText: string): string;
var
  aTempDataSet: TADODataSet;
begin
  try
    if OpenDataSet(aTempDataSet, SqlText) > 0 then
    begin
      Result := aTempDataSet.Fields[0].AsString;
    end;
  finally
    FreeAndNil(aTempDataSet);
  end;
end;


function TdbHelper.IsEmpty(SqlText: string): Boolean;
var
  aTempDataSet: TADODataSet;
begin
  try
    Result := OpenDataSet(aTempDataSet, SqlText) > 0;
  finally
    FreeAndNil(aTempDataSet);
  end;
end;








procedure TdbHelper.DataModuleCreate(Sender: TObject);
begin
  conSql.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DB\WTPARK.mdb;Persist Security Info=False';
  conSql.Connected := True;
end;

end.

